<?php
namespace Book\Controller;

use Wechat\Controller\WxCustomerBaseController;
use Org\Wechat\JSSDK;

class IndexController extends WxCustomerBaseController {
    
    protected $bookCatModel;
	protected $bookModel;
	protected $bookCodeModel;
	
	function _initialize() {
		parent::_initialize();
		
		$this->bookCatModel = D("Book/BookCat");
		$this->bookModel = D("Book/Book");
		$this->bookCodeModel = D("Book/BookCode");
	}
	
	/******************************** JSON *********************************/
	/** 添加信息 */
	public function save() {
	    ////// validate
// 	    $rules = array(
// 	        // array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
// 	        array('season_id', 'require', '分期未选择！', 1),
// 	        array('master_name', 'require', '联系人姓名不能为空！', 1),
// 	        array('master_tel', 'require', '联系人手机号不能为空！', 1),
// 	        array('season_count', 'number', '排序必须是数字。', 2),                  // 活动数量大于0
// 	    );
// 	    if ($this->regOrderModel->validate($rules)->create() === false) {
//             $this->assign("messageTitle", '添加订单出错！');
// 			$this->error($this->regOrderModel->getError());
//             return;
// 	    }

	    $code = I('post.code');
	    $book['isbn'] = I('post.isbn');
	    $book['name'] = I('post.name');
	    $book['author'] = I('post.author');
	    $book['translator'] = I('post.translator');
	    $book['pages'] = I('post.pages');
	    $book['price'] = I('post.price');
	    $book['publisher'] = I('post.publisher');
	    $book['pubdate'] = I('post.pubdate');
	    $book['summary'] = I('post.summary');
	    $book['content'] = '';
	    $book['thumb'] = I('post.thumb');
	    $book['cid'] = I('post.cid');
	    $book['cname'] = I('post.cname');
	    $book['lv'] = I('post.lv');
	    
		// 检查code是否已经存在
		if ($this->bookCodeModel->where(array("code" => $code))->find()) {
			$data['code'] = 499;
			$data['msg'] = '添加失败！';
			$this->ajaxReturn($data);
		}
		
		// 如果图书是第一次被录入则添加book及book code，否则更新book，并添加book code
		$oldBook = $this->bookModel->where(array("isbn" => $book['isbn']))->find();
		if (!$oldBook) {
			// save book
			$book['code_count'] = 1;
			$book['remain_count'] = 1;
			$book['fav_count'] = 0;
			$book['view_count'] = 0;
            $book['add_time'] = date("Y-m-d H:i:s");
            $book['update_time'] = date("Y-m-d H:i:s");
            $result = $this->bookModel->add($book);

			// save book code
			$bookCode['code'] = $code;
			$bookCode['bid'] = $result;
			$bookCode['bname'] = $book['name'];
            $book['add_time'] = date("Y-m-d H:i:s");
            $book['update_time'] = date("Y-m-d H:i:s");
            $result = $this->bookCodeModel->add($bookCode);

    	    $data['code'] = 0;
    	    $data['msg'] = '添加成功!';
    	    $data['obj'] = $book;
    	    $this->ajaxReturn($data);
		} else {
			// update book
		    $oldBook['code_count']++;
		    $oldBook['remain_count']++;
            $book['update_time'] = date("Y-m-d H:i:s");
            $result = $this->bookModel->where(array("id" => $oldBook['id']))->save($oldBook);
			
			// save book code
			$bookCode['code'] = $code;
			$bookCode['bid'] = $oldBook['id'];
			$bookCode['bname'] = $book['name'];
            $book['add_time'] = date("Y-m-d H:i:s");
            $book['update_time'] = date("Y-m-d H:i:s");
            $result = $this->bookCodeModel->add($bookCode);

    	    $data['code'] = 0;
    	    $data['msg'] = '添加成功!';
    	    $data['obj'] = $oldBook;
    	    $this->ajaxReturn($data);
		}
	}
	
    /** 查看某信息ByCode */
	public function findByCode() {
	    ///// find bookCode
	    $code = I('code');
	    $bookCode = $this->bookCodeModel->where(array("code" => $code))->find();
	    if (!$bookCode) {
	        $data['code'] = 499;
	        $data['msg'] = '图书不存在!';
	        $this->ajaxReturn($data);
	    }

	    ///// find book
	    $book = $this->bookModel->where(array("id" => $bookCode['bid']))->find();
	    if (!$book) {
	        $data['code'] = 499;
	        $data['msg'] = '图书不存在!';
	        $this->ajaxReturn($data);
	    }

	    $data['code'] = 0;
	    $data['msg'] = '找到图书!';
	    $data['obj'] = $book;
	    $this->ajaxReturn($data);
    }

}
